/**
 * org.assess.oa.system.service.impl.TbDepartmentServiceImpl
 */
package org.assess.oa.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.assess.core.common.DateUtils;
import org.assess.core.service.BaseServiceImpl;
import org.assess.oa.system.bean.TbDepartment;
import org.assess.oa.system.dao.TbDepartmentDAO;
import org.assess.oa.system.service.TbDepartmentService;

import com.alibaba.fastjson.JSON;

/**
 * @description 部门管理的Service层实现类
 * @author XiaLongxiang
 * @createtime 2012-5-15 上午9:26:34
 */
public class TbDepartmentServiceImpl extends BaseServiceImpl implements TbDepartmentService {
    /** 日志对象 */
    private static Logger logger = Logger.getLogger(TbDepartmentServiceImpl.class);
    
    /** 操作部门表的DAO层对象 */
    private TbDepartmentDAO tbDepartmentDAO;
    
    public String queryTbDepartmentAsList(TbDepartment department) {
        String result = "";
        try {
            Map<String, Object> parameters = new HashMap<String, Object>();
            if (department != null) {
                parameters.put("parent_code", department.getParent_code());
                parameters.put("valid_flag", department.getValid_flag());
            }
            
            List<TbDepartment> records = this.tbDepartmentDAO.queryTbDepartmentAsList(parameters);
            result = super.toListPageString(1, 1, 1, "getDepartment_code", records);
        }
        catch (Exception ex) {
            logger.error(ex);
        }
        return result;
    }
    
    public String queryTbDepartmentAsStartPrior(TbDepartment department) {
        String result = "";
        try {
            Map<String, Object> parameters = new HashMap<String, Object>();
            if (department != null) {
                parameters.put("valid_flag", department.getValid_flag());
            }
            List<TbDepartment> records = this.tbDepartmentDAO.queryTbDepartmentAsStartPrior(parameters);
            result = this.toTreePageString(records);
        }
        catch (Exception ex) {
            logger.error(ex);
        }
        
        return result;
    }
    
    public String queryDepartmentAsJsonArray(TbDepartment department) {
        String result = "";
        try {
            Map<String, Object> parameters = new HashMap<String, Object>();
            if (department != null) {
                parameters.put("parent_code", department.getParent_code());
                parameters.put("valid_flag", department.getValid_flag());
            }
            
            List<TbDepartment> records = this.tbDepartmentDAO.queryTbDepartmentAsList(parameters);
            result = JSON.toJSONString(records);
        }
        catch (Exception ex) {
            logger.error(ex);
        }
        return result;
    }
    
    public String updateValid(TbDepartment department) {
        String result = "";
        try {
            String[] department_codes = department.getDepartment_code().split(",");
            department.setModify_time(new Date());
            department.setModify_user(super.getLoginUserCode());
            for (String code : department_codes) {
                department.setDepartment_code(code);
                this.tbDepartmentDAO.updateTbDepartmentValid(department);
            }
            
            result = super.toResultString(true);
        }
        catch (Exception ex) {
            logger.error(ex);
        }
        
        return result;
    }
    
    private String toTreePageString(List<TbDepartment> records) {
        StringBuffer result = new StringBuffer();
        Map<String, TbDepartment> map = new HashMap<String, TbDepartment>();
        if (records != null && !records.isEmpty()) {
            for (TbDepartment dept : records) {
                map.put(dept.getDepartment_code(), dept);
            }
        }
        
        List<TbDepartment> _tempList = new ArrayList<TbDepartment>();
        for (TbDepartment dept : records) {
            TbDepartment parent = map.get(dept.getParent_code());
            if (parent != null) {
                parent.addChild(dept);
            }
            else {
                _tempList.add(dept);
            }
        }
        
        result.append("{total:1,page:1,records:").append(records.size()).append(",rows:[");
        for (TbDepartment dept : _tempList) {
            result.append(this.asTreeString(dept, 0)).append(",");
        }
        if (!_tempList.isEmpty()) {
            result.delete(result.length() - 1, result.length());
        }
        return result.append("]}").toString();
    }
    
    private String asTreeString(TbDepartment dept, int depth) {
        StringBuffer result = new StringBuffer();
        result.append("{id:").append(dept.getDepartment_code()).append(",cell:[");
        result.append("\"").append(dept.getDepartment_name()).append("\",");
        result.append("\"").append(dept.getParent_name() != null ? dept.getParent_name() : "").append("\",");
        result.append("\"").append(dept.getCreate_time() != null ? DateUtils.getTime(dept.getCreate_time()) : "")
                .append("\",");
        result.append("\"").append(dept.getModify_time() != null ? DateUtils.getTime(dept.getModify_time()) : "")
                .append("\",");
        result.append("\"").append(depth).append("\",");
        result.append("\"").append(dept.getParent_code() != null ? dept.getParent_code() : "").append("\",");
        result.append("\"").append(dept.getChildren() == null || dept.getChildren().isEmpty()).append("\",");
        result.append("\"false\"");
        result.append("]}");
        if (dept.getChildren() != null && !dept.getChildren().isEmpty()) {
            result.append(",");
            for (TbDepartment node : dept.getChildren()) {
                result.append(this.asTreeString(node, depth + 1)).append(",");
            }
            result.delete(result.length() - 1, result.length());
        }
        return result.toString();
    }
    
    public void setTbDepartmentDAO(TbDepartmentDAO tbDepartmentDAO) {
        this.tbDepartmentDAO = tbDepartmentDAO;
    }
}
